local xiangxia = fk.CreateSkill{
  name = "wk_heg__xiangxia",
}
Fk:loadTranslationTable{
  ["wk_heg__xiangxia"] = "相狭",
  [":wk_heg__xiangxia"] = "锁定技，其他角色获得你的牌后，你获得其等量的牌；其他角色弃置你的牌后，你弃置其等量的牌。",

  ["#wk_heg__xiangxia"] = "相狭：%arg%dest的%arg2张牌",
  ["wk_heg__xiangxia_aquire"] = "获得",
  ["wk_heg__xiangxia_dicard"] = "弃置"
}

xiangxia:addEffect(fk.AfterCardsMove, {
  can_trigger = function (self, event, target, player, data)
    if not player:hasSkill(self) then return end
    for _, move in ipairs(data) do
      if move.from == player then
        if move.to and move.to ~= player or (move.toArea == Card.DiscardPile and move.proposer ~= player) then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
              return true
            end
          end
        end
      end
    end
  end,
  on_trigger = function (self, event, target, player, data)
    local is_discard = nil
    local aquire = {} ---@type table<ServerPlayer, integer>
    local discard = {} ---@type table<ServerPlayer, integer>
    for _, move in ipairs(data) do
      if move.from == player then
        if move.to and move.to ~= player and (move.toArea == Player.Hand or move.to == Player.Equip) then
          is_discard = false
        elseif move.toArea == Card.DiscardPile and move.proposer and move.proposer ~= player then
          is_discard = true
        end
        if is_discard ~= nil then
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Player.Hand or info.fromArea == Player.Equip then
              if is_discard then
                discard[move.proposer] = (discard[move.proposer] or 0) + 1
              else
                aquire[move.to] = (aquire[move.to] or 0) + 1
              end
            end
          end
        end
      end
    end
    player.room:setPlayerMark(player, "wk_heg__xiangxia_is_discard", 0)
    for p, n in pairs(aquire) do
      player.room:setPlayerMark(player, "wk_heg__xiangxia_n", n)
      player.room:setPlayerMark(player, "wk_heg__xiangxia_p", p.id)
      self:doCost(event, target, player, data)
    end
    player.room:setPlayerMark(player, "wk_heg__xiangxia_is_discard", 1)
    for p, n in pairs(discard) do
      player.room:setPlayerMark(player, "wk_heg__xiangxia_n", n)
      player.room:setPlayerMark(player, "wk_heg__xiangxia_p", p.id)
      self:doCost(event, target, player, data)
    end
  end,
  on_cost = Util.TrueFunc,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local n = player:getMark"wk_heg__xiangxia_n"
    local p = room:getPlayerById(player:getMark"wk_heg__xiangxia_p")
    local is_discard = player:getMark"wk_heg__xiangxia_is_discard"
    local num = math.min(player:getHandcardNum(), n)
    local action = "wk_heg__xiangxia_aquire"
    if is_discard == 1 then
      action = "wk_heg__xiangxia_dicard"
    end
    local cards = room:askToChooseCards(player, {
      target = p,
      min = num,
      max = num,
      flag = "he",
      skill_name = xiangxia.name,
      prompt = "#wk_heg__xiangxia::"..p.id..":"..action..":"..num
    })
    if is_discard == 1 then
      room:throwCard(cards, xiangxia.name, p, player)
    else
      room:obtainCard(player, cards, false, fk.ReasonPrey, player, xiangxia.name)
    end
  end
})

return xiangxia